<?php

/**
 * Created by ymk
 * User: Administrator
 * Date: 2018/6/29
 * Time: 18:17
 */

namespace MeetUp\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Base extends Model
{
    use SoftDeletes;//软删除

    protected $dates = ['deleted_at'];

    /**
     * 自动格式化时间戳
     * @param \DateTime|int $value
     *
     * @return false|int
     */
    public function fromDateTime($value){
        return strtotime(parent::fromDateTime($value));
    }
    //时间显示多久之前(21分钟之前发表)
    public function formatDate($time = 0){
        $time = strtotime($time);
        $tmp = time() - $time;
        if($tmp && $tmp <= 60 * 60){
            return ceil($tmp / 60) + 1 . '分钟前';
        }elseif($tmp > 60 * 60 && $tmp < 60 * 60 * 24){
            return ceil($tmp / 60 / 60) + 1 . '小时前';
        }elseif($tmp > 60 * 60 * 24 && $tmp < 60 * 60 * 24 * 30){
            return ceil($tmp / 60 / 60 / 24) + 1 . '天前';
        }else{
            return date('Y-m-d H:i:s',$time);
        }
    }
}